package service.impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.User;
import service.UserService;

public class UserServiceImpl implements UserService {

    private final UserDao userDao = new UserDaoImpl();

    @Override
    public int login(String username, String password) {
        // Validate input
        if (username == null || username.trim().isEmpty()) {
            return 0; // treat as "account not found / invalid input"
        }

        User user = userDao.getUserByUsername(username);
        // If DAO returns null when user not found, handle it
        if (user == null) {
            return 0; // account not found
        }

        // Compare passwords null-safely
        String stored = user.getPassword();
        if (stored != null && stored.equals(password)) {
            return 1; // success
        }

        return 2; // password incorrect
    }
}
